//
//  Photo.m
//  TestPhotoMS
//
//  Created by Hung Nguyen Thanh on 10/2/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "Photo.h"

@implementation Photo

@synthesize image, thumbImage, link, title, author, idLink, imageID, category, fileName, like, dislike;
@synthesize latitude, longitude;

- (void)setLink:(NSString *)aLink{
    if (self->link != nil) {
        self->link = [[NSString alloc] initWithString:[link stringByAppendingString:aLink]];
    } else {
        link = [[NSString alloc] initWithString:aLink];
    }
}

- (void)setTitle:(NSString *)atitle{
    if (title != nil) {
        title = [[NSString alloc] initWithString:[title stringByAppendingString:atitle]];
    } else {
        title = [[NSString alloc] initWithString:atitle];
    }
    title = [title stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" \n\r\t"]];
}

- (void)setAuthor:(NSString *)aauthor{
    if (author != nil) {
        author = [[NSString alloc] initWithString:[author stringByAppendingString:aauthor]];
    } else {
        author = [[NSString alloc] initWithString:author];
    }
}

- (void)setIdLink:(NSString *)aidLink{
    if (idLink != nil) {
        idLink = [[NSString alloc] initWithString:[idLink stringByAppendingString:aidLink]];
    } else {
        idLink = [[NSString alloc] initWithString:aidLink];
    }
}
- (void)setCategory:(NSString *)acategory{
    if (category != nil) {
        category = [[NSString alloc] initWithString:[category stringByAppendingString:acategory]];
    } else {
        category = [[NSString alloc] initWithString:acategory];
    }
}

- (void)setFileName:(NSString *)afileName{
    if (fileName != nil) {
        fileName = [[NSString alloc] initWithString:[fileName stringByAppendingString:[self trim:afileName]]];
    } else {
        fileName = [[NSString alloc] initWithString:[self trim:afileName]];
    }
}

+ (NSData *)getImageData:(NSString *)surl{
//    NSLog(@"%@", surl);
    NSURL *url = [NSURL URLWithString:surl];
    return [NSData dataWithContentsOfURL:url];
}

- (UIImage *)thumbImage{
    if (thumbImage == nil) {
        NSString *imageURL = [GET_PHOTO_THUMBNAIL stringByAppendingString:self.fileName];
        
        thumbImage = [UIImage imageWithData:[Photo getImageData:imageURL]];
    }
    return thumbImage;
}
 


- (UIImage *)image{
    NSData *imageData = [Photo getImageData:[NSString stringWithFormat:@"%@%@", GET_PHOTO, self.fileName]];
    return [UIImage imageWithData:imageData];
}

- (NSString *)trim:(NSString *)text{
    return [text stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" \t\r\n"]];
}
@end
